package com.itboye.jinpuyiyuan.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.activity.home.OrderDetailActivity;
import com.itboye.jinpuyiyuan.adapter.IllRecordAdapter;
import com.itboye.jinpuyiyuan.adapter.MeHosAdapter;
import com.itboye.jinpuyiyuan.adapter.PaymentRecordAdapter;
import com.itboye.jinpuyiyuan.adapter.PrescriptionRecordAdapter;
import com.itboye.jinpuyiyuan.base.BaseActivity;
import com.itboye.jinpuyiyuan.bean.HospRecord;
import com.itboye.jinpuyiyuan.bean.OrderBean;
import com.itboye.jinpuyiyuan.bean.OrderList;
import com.itboye.jinpuyiyuan.bean.RecipeRecordBean;
import com.itboye.jinpuyiyuan.presenter.BranchAddressPresenter;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;
import com.itboye.jinpuyiyuan.utils.volley.ResultEntity;
import com.itboye.jinpuyiyuan.widget.loadingutil.MAlert;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * Created by Administrator on 2018/1/10 0010.
 */

public class PrescriptionRecordActivity extends BaseActivity implements Observer {
    //title
    private ImageView leftImg,rightImg;
    private TextView leftTv,tvTitle;
    private RelativeLayout rlLeft,toobar;


    private List<RecipeRecordBean.PayListBean> list;
    private BranchAddressPresenter presenter;
    private String uid;

    private SmartRefreshLayout refresh;
    private RecyclerView inHos;

    private MeHosAdapter meHosAdapter;


    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.activity_ill);


        rightImg.setVisibility(View.GONE);
        leftTv.setVisibility(View.GONE);
        tvTitle.setText("处方记录");
        uid= SPUtils.get(this,null, Const.UID,"")+"";
        presenter=new BranchAddressPresenter(this);
        list=new ArrayList<>();

//        adapter=new PrescriptionRecordAdapter(this,list,R.layout.item_panment_records);
//        payment_list.setAdapter(adapter);

//        setRefresh();
//        refresh.autoRefresh();

//        payment_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                    Intent intent = new Intent(PrescriptionRecordActivity.this, PrescriptionRecordInfo.class);
//                    intent.putExtra("hosp_sequence",list.get(position).getHosp_sequnce());
//                    intent.putExtra("order_stutas",list.get(position).getOrder_status());
//                    intent.putExtra("payType",list.get(position).getPay_channel_id());
//                    intent.putExtra("date",list.get(position).getReceipt_date());
//                    startActivity(intent);
//
//            }
//        });

        meHosAdapter=new MeHosAdapter(this,list,2,0.3f);
        meHosAdapter.setOnItemClick(new MeHosAdapter.OnItemClick() {
            @Override
            public void setOnItemClic(int position) {
                    Intent intent = new Intent(PrescriptionRecordActivity.this, PrescriptionRecordInfo.class);
                    intent.putExtra("hosp_sequence",list.get(position).getHosp_sequnce());
                    intent.putExtra("order_stutas",list.get(position).getOrder_status());
                    intent.putExtra("payType",list.get(position).getPay_channel_id());
                    intent.putExtra("date",list.get(position).getReceipt_date());
                    startActivity(intent);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        inHos.setLayoutManager(linearLayoutManager);
        inHos.setAdapter(meHosAdapter);


        refresh.setEnableLoadmore(false);
//        WaveSwipeHeader waveSwipeHeader = new WaveSwipeHeader(this);
//        waveSwipeHeader.setColorSchemeColors(getResources().getColor(R.color.colorAccent));

        refresh.autoRefresh();
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                presenter.recipeRecord(uid,"0");
            }
        });

        ClassicsHeader classicsHeader = new ClassicsHeader(this);
        ClassicsFooter classicsFooter = new ClassicsFooter(this);

        refresh.setRefreshHeader(classicsHeader);
        refresh.setRefreshFooter(classicsFooter);
        showProgressDialog("数据获取中,请稍等...",false);

    }

//    public void setRefresh(){
//
//            refresh.setLoadMore(false);
//            refresh.setMaterialRefreshListener(new MaterialRefreshListener() {
//                @Override
//                public void onRefresh(final MaterialRefreshLayout materialRefreshLayout) {
//                presenter.recipeRecord(uid,"0");
//                }
//
////                @Override
////                public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
////                    page++;
////                    isLoadMore=true;
////                    presenter.getOrderList(uid,order_type,"-1",page+"","10");
////
////                }
//            });
//        showProgressDialog("数据获取中,请稍等...",false);
//
//    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.rlLeft:
            case R.id.leftImg:
                this.finish();
                break;

        }
    }
    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = handlerError(arg);

        if (entity!=null){
            try{
                refresh.finishRefresh();
                closeProgressDialog();

            }catch (Exception e){

            }
            if (entity.getEventType() == BranchAddressPresenter.By_prescriptionRecord_success){ //处方记录
                RecipeRecordBean orderList= (RecipeRecordBean) entity.getData();
                List<RecipeRecordBean.PayListBean> orders = orderList.getPay_list();

                    if (this.list.size()>0){
                        this.list.clear();
                    }
                    this.list.addAll(orders);
                    meHosAdapter.addData(list,2,0.3f);
            }else  if (entity.getEventType() == BranchAddressPresenter.By_prescriptionRecord_fail){
                MAlert.alert(entity.getMsg());

            }
        }
    }

}
